package com.pet.server.ext.sms;

//import java.util.ArrayList;
//import java.util.List;

//import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
//import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
//import com.gexin.rp.sdk.template.NotyPopLoadTemplate;
//import com.gexin.rp.sdk.template.PopupTransmissionTemplate;
//import com.gexin.rp.sdk.template.TransmissionTemplate;

public class PushHelper {
    
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";
        
    // android test
//    static String appkey = "6RlKspCM6nAhXYPp0FFWG8";
//    static String master = "5pLX5JRFEo8oswIlJjUAZ8";
//    static String appId = "YQDjPPhfyj6WG15brq7zn6";
    
//    static String CID = "0fe55552a608cc65a0660c7cc5ad385a";
    
    // ios test
    static String appkey = "SSXCYhC6EA7neeM98peDP4";
    static String master = "hfXEKeMBra7cKTrlEvNXu3";
    static String appId = "Uniwhf4PIc79fkXyROjjA3";
    
    // huaqianshu app v1.0
//    static String appkey = "LojzVA1r0l9Y6MgXAr5Y66";
//    static String master = "7QpCg5QJfW6BBdR7YCXW45";
//    static String appId = "4xLBmV3Gci9LCecvapBSz2";
        
    
//  chenxiaobin  
//    static String CID = "dccf45c63d8dd03e1956e1ddbe297d53";
    
//  lijiayu    
//    static String CID = "8ba7c86a4326fd8707fe2e545e6359ab";
    
//  tanglei    
//    static String CID = "fd49c5f7e742d7485fd85fe04ff28cdd";
    
//    hj
//    static String CID = "767f20a095263de811a7b238095fc780";
    
    //zhaoyuguang
//    static String CID = "4317ff1825cbafc3ec928a379f9c6c8e";

    // dengyuguang test
//    static String appkey = "SSXCYhC6EA7neeM98peDP4";
//    static String master = "ZdqGnfF8KLAqLUI2SijWW7";
//    static String appId = "Uniwhf4PIc79fkXyROjjA3";
//    
    static String CID = "3fcccf274e3cc63c3b95c335f5cf8973";//"67421d70a6e3ab848a85d6432f411d41";
    
    // lipan
//    static String CID = "5b772a33d4f285d14440e9e31d80f900";
    
    
//  static String host = "http://192.168.10.61:8006/apiex.htm";
//  static String host = "http://edj-s.open.api.igexin.com:8006/apiex.htm";
    

    // getui demo
//    static String master = "6bLzdFZWFeAlPrjNms4Aq7";
//    static String appId = "etVyv7RHJ28BqN3KKCtKJ6";
//    static String appkey = "R03r2hiQI18UxbaFBZkRw";
    
//  hujing@getui demo  
//    static String CID = "059960f699965f9d764ec222864812a0";
    
    public static void push(String cid, String title, String msgContent, String transContent) throws Exception {
//    	if (logcat != null) {
//	        logcat = logcat.derive(PushHelper.class);
//	    	logcat.debug("pushing message to (%s) with title: %s", cid, title);
//    	}
//    	if (cid == null || cid.length() == 0) {
//        	if (logcat != null) {
//        		logcat.debug("ignore pushing for no CID");        		
//        	}
//            return;
//        }
        IGtPush push = new IGtPush(host, appkey, master);
        push.connect();

//        NotificationTemplate template = createNotificationTemplate(cid, title, msgContent, transContent);
        TransmissionTemplate template = createTransmissionTemplate(cid, title, msgContent, transContent);
        
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        message.setOfflineExpireTime(72 * 3600 * 1000);
        message.setData(template);

        Target target = new Target();
        target.setClientId(cid);
        target.setAppId(appId);
        
        IPushResult ret = push.pushMessageToSingle(message, target);
//        if (logcat != null) {
//            logcat.debug("pushing result: %s", ret.getResponse().toString());
//        }else{
//        	
//        }
////      System.out.println(ret.getResponse().get("status").equals("successed_offline"));
//        }
    }

    private static NotificationTemplate createNotificationTemplate(String cid, String title, String msgContent, String transContent) throws Exception {
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appkey);
        template.setTitle(title);
        template.setText(msgContent);
//        template.setLogo("icon.png");
//        template.setLogoUrl("");
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        template.setTransmissionType(1);
        template.setTransmissionContent(transContent);
        template.setPushInfo("actionLocKey", 1, transContent, "sound", "payload", msgContent, "locArgs", "launchImage");
        return template;
    }
    
    private static TransmissionTemplate createTransmissionTemplate(String cid, String title, String msgContent, String transContent) throws Exception {
		TransmissionTemplate template = new TransmissionTemplate();
		template.setAppId(appId);
		template.setAppkey(appkey);
		template.setTransmissionType(2);
		
		String content = String.format("{\"title\":\"%s\", \"content\":\"%s\", \"operation\":%s}", title, msgContent, transContent);
		template.setTransmissionContent(content);
		template.setPushInfo("actionLocKey", 1, transContent, "sound", "payload", title, "locArgs", "launchImage");
		return template;
	}
    
    public static void main(String[] args)throws Exception {
    	String[][] data = new String[][]{
//    			{"您有新订单！", "有个新订单适合您配送，快去看看吧！", "{\"action\":\"new\"}"},
    			{"您的帐号已通过审核！", "请重新登录！之后即可发单和接单。", "{\"action\":\"authenticate\"}"},
//    			{"您的订单(201410123456)审核已通过！", "请修改订单。", "{\"action\":\"approved\", \"order_id\":201410123456}"},
//    			{"订单(201410123456)审核未过！", "一二三四五六七八九十", "{\"action\":\"editOrder\", \"order_id\":201410123456}"},
//    			{"恭喜中标！", "您已赢得订单(201410123456)！", "{\"action\":\"win\", \"order_id\":201410123456}"},
//    			{"有花店竞标！", "请查看订单(201410123456)！", "{\"action\":\"bid\", \"order_id\":201410123456}"},
//    			{"订单配送完成！", "订单号(201410123456)。", "{\"action\":\"delivered\", \"order_id\":201410123456}"}
    	};
    	
//        String title = "您有新订单！";
//        String msgContent = "有个新订单适合您配送，快去看看吧！";
//        String transContent = "{\"action\":\"new\"}";

//        String title = "您的帐号已通过审核！";
//        String msgContent = "请重新登录！之后即可发单和接单。";    
//        String transContent = "{\"action\":\"authenticate\"}";
        

//    	String title = "您的订单(201410123456)审核已通过！";
//        String msgContent = "请修改订单。";
//        String transContent = "{\"action\":\"approved\", \"order_id\":201410123456}";
//
//    	String title = "您的订单(201410123456)审核未通过！";
//        String msgContent = "请修改订单。";
//        String transContent = "{\"action\":\"editOrder\", \"order_id\":201410123456}";

//      String title = "恭喜中标！";
//      String msgContent = "您已赢得订单(201410123456)！";
//      String transContent = "{\"action\":\"win\", \"order_id\":201410123456}";

//        String title = "有花店竞标！";
//        String msgContent = "请查看订单(201410123456)！";
//        String transContent = "{\"action\":\"bid\", \"order_id\":201410123456}";

//        String title = "订单配送完成！";
//        String msgContent = "订单号(201410123456)。";
//        String transContent = "{\"action\":\"delivered\", \"order_id\":201410123456}";
//        
    	for (int i = 0; i < 10; i ++) {
	    	for (String[] item : data) {
	    		String title = item[0];
	    		String msgContent = item[1];
	    		String transContent = item[2];
	        	push(CID, title, msgContent, transContent);
	            break;
//	        	Thread.sleep(60000);            
	    	}
	    	break;
    	}
    }
}
